package yixing.activity;

import yixing.highscore.HighscoreAdapter;
import yixing.util.Constant;
import yixing.util.ShareSimpleData;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.CheckBox;
import android.widget.Toast;

public class StoreActivity extends Activity{

	CheckBox flyTools, doubleFlyTools, resurrectionTools, doubleCoinTools;
	
	@Override
    public void onCreate(Bundle savedInstanceState) {
    	requestWindowFeature(Window.FEATURE_NO_TITLE);  
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
		
    	super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_store); 
        
        flyTools = (CheckBox) findViewById(R.id.fly_second);
        doubleFlyTools = (CheckBox) findViewById(R.id.double_fly_second);
        resurrectionTools = (CheckBox) findViewById(R.id.resurrection_second);
        doubleCoinTools = (CheckBox) findViewById(R.id.double_coin_second);
	}

	public void backBtnClick(View view){
		this.finish();
	}
	
	int totalCoin = 0, toolsNumber = 0, flyToolsNumber = 0, resurrectionToolsNumber = 0, doubleCoinToolsNumber = 0, doubleFlyToolsNumber = 0;
		
	public void yesBtnClick(View view){
		
		if(flyTools.isChecked()) {
			
			flyToolsNumber = 10;
			toolsNumber++;
		}
			
		if(resurrectionTools.isChecked()){
			
			resurrectionToolsNumber = 5;
			toolsNumber++;
		}
		
		if(doubleCoinTools.isChecked()){
			
			doubleCoinToolsNumber = 5;
			toolsNumber++;
		}

		if(doubleFlyTools.isChecked()){
			
			doubleFlyToolsNumber = 10;
			toolsNumber++;
		}
		
		if(toolsNumber == 0){
			
			Toast.makeText(getApplicationContext(), 
					"请在要购买的任一方框中打钩！", Toast.LENGTH_SHORT).show();
		}else{
					
			totalCoin = 5000 * toolsNumber;
			
			popWindow(totalCoin, flyToolsNumber, resurrectionToolsNumber, doubleCoinToolsNumber, doubleFlyToolsNumber);
		}
	}
	
	ShareSimpleData shareSimpleData;
	
	private void popWindow(final int coin, final int fly, 
			final int resurrection, final int doubleCoin, final int doubleFly){
	
		final Context contxt = StoreActivity.this;
		HighscoreAdapter score = new HighscoreAdapter(StoreActivity.this);
        score.open();
        shareSimpleData = new ShareSimpleData(StoreActivity.this);
        final int scores = score.getScoreTotal() - 
        		shareSimpleData.getInt(Constant.USED_COIN_TAG);

        score.close();
        
		final AlertDialog.Builder builder = new AlertDialog.Builder(contxt);
		
		if(scores < coin)
			builder.setTitle("金币不够，再玩几局吧！");
		else
			builder.setTitle("确定花费" + coin + "个金币购买？");
	
		builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {

			public void onClick(DialogInterface dialog, int which) {
				
				if(scores >= coin){
					shareSimpleData.putInt(Constant.USED_COIN_TAG,  coin + 
							shareSimpleData.getInt(Constant.USED_COIN_TAG));
					
					shareSimpleData.putInt(Constant.FLY_TAG, fly + shareSimpleData.getInt(Constant.FLY_TAG));
					shareSimpleData.putInt(Constant.DOUBLE_COIN_TAG, doubleCoin + shareSimpleData.getInt(Constant.DOUBLE_COIN_TAG));
					shareSimpleData.putInt(Constant.RESURRECTION_TAG, resurrection + shareSimpleData.getInt(Constant.RESURRECTION_TAG));
					shareSimpleData.putInt(Constant.DOUBLE_FLY_TAG, doubleFly + shareSimpleData.getInt(Constant.DOUBLE_FLY_TAG));
				}
			}
		});
		
		builder.setNegativeButton("返回", new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int which) {
				
				Toast.makeText(getApplicationContext(), "取消购买！", Toast.LENGTH_SHORT).show();
				
				toolsNumber = 0;
			}
		});
		
		final AlertDialog dialogs = builder.create();

		dialogs.show();
		dialogs.setCanceledOnTouchOutside(false);
	}
}
